﻿Namespace Control
    Public Class Empresa
        Implements IControl(Of Model.Pessoa)
        Implements IDisposable


        Public Function Excluir(ByVal entity As Model.Pessoa) As Boolean Implements IControl(Of Model.Pessoa).Excluir
            Dim Result As Boolean = True

            Try
                entity.Delete()
            Catch ex As Exception
                Dialog.Alert(ex.Message)
                Result = False
            End Try

            Return Result
        End Function

        Public Function ListaTudo() As System.Collections.Generic.IList(Of Model.Pessoa) Implements IControl(Of Model.Pessoa).ListaTudo
            'Dim pes As New Model.Pessoa
            'Return pes.GetByTipoCliente(Tipo.)
            Dim pes As New Model.Pessoa

            Return pes.GetByTipo(Model.Pessoa.PessoaTipo.Empresa)
        End Function

        Public Function ListaUm(ByVal entity As Model.Pessoa) As Model.Pessoa Implements IControl(Of Model.Pessoa).ListaUm
            Return Model.Pessoa.Find(entity.cd_pessoa)
        End Function

        Public Function Salvar(ByVal entity As Model.Pessoa) As Boolean Implements IControl(Of Model.Pessoa).Salvar
            Try
                entity.pessoa_empresa = Model.Pessoa.PessoaTipoStatus.Definido 'Model.Pessoa.PessoaTipo.Empresa

                ' Edson Tessaro - 15/01/2013
                ' entity._sessionScope.Flush()
                ' entity.Save()

                entity.SaveAndFlush()
            Catch ex As Exception
                Dialog.Alert(ex.Message)
            End Try

            Return True
            'Dim pes As New Model.Pessoa
            'Return pes.Salvar(_Pessoa)
        End Function


        Public Function ListaTudoPesquisa() As IList(Of Model.PessoaPesquisa)
            'Dim pes As New Model.Pessoa
            'Return pes.GetByTipoCliente(Tipo.)
            Dim pes As New Model.PessoaPesquisa

            Return pes.GetByTipo(Model.PessoaPesquisa.PessoaTipo.Empresa)
        End Function

        Public Function ListaByNomeCPFCNPJ(Optional ByVal Nome As String = "", Optional ByVal CPFCNPJ As String = "") As IList(Of Model.PessoaPesquisa)
            Dim pes As New Model.PessoaPesquisa

            If Not String.IsNullOrEmpty(Nome) Then
                Return pes.GetByNome(Model.PessoaPesquisa.PessoaTipo.Empresa, Nome)
            ElseIf Not String.IsNullOrEmpty(CPFCNPJ) Then
                Return pes.GetByCPFCNPJ(Model.PessoaPesquisa.PessoaTipo.Empresa, CPFCNPJ)
            Else
                Return ListaTudoPesquisa()
            End If

        End Function

        ''' <summary>
        ''' Pesquisa por descrição
        ''' </summary>
        ''' <param name="Descricao">Texto a ser pesquisado</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function ListaByDescricao(Optional ByVal Descricao As String = "") As IList(Of Model.Pessoa)
            Dim pess As New Model.Pessoa

            If Not String.IsNullOrEmpty(Descricao) Then
                Return pess.GetByNome(Model.Pessoa.PessoaTipo.Empresa, Descricao)
            Else
                Return pess.GetByTipo(Model.Pessoa.PessoaTipo.Empresa)
            End If

        End Function

        Public Function ExcluirPesquisa(ByVal entity As Model.PessoaPesquisa) As Boolean
            Dim Result As Boolean = True

            Try
                entity.Delete()
            Catch ex As Exception
                Dialog.Alert(ex.Message)
                Result = False
            End Try

            Return Result
        End Function

        Public Function GetById(ByVal id As Integer) As Model.Pessoa
            Return Model.Pessoa.Find(id)
        End Function

#Region "IDisposable Support"
        Private disposedValue As Boolean ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: dispose managed state (managed objects).
                End If

                ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
                ' TODO: set large fields to null.
            End If
            Me.disposedValue = True
        End Sub

        ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
        'Protected Overrides Sub Finalize()
        '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        '    Dispose(False)
        '    MyBase.Finalize()
        'End Sub

        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class
End Namespace